package com.zhanghu.backendZray.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.converter.NotifySettingConverter;
import com.zhanghu.backendZray.enums.NotifyTypeEnum;
import com.zhanghu.backendZray.pojo.entity.NotifySetting;
import com.zhanghu.backendZray.mapper.NotifySettingMapper;
import com.zhanghu.backendZray.pojo.request.mail.NotifySettingRequest;
import com.zhanghu.backendZray.pojo.vo.mail.NotifySettingVo;
import com.zhanghu.backendZray.service.NotifySettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 用户邮箱配置 服务实现类
 * </p>
 *
 * @author zhangdong
 * @since 2024-08-28
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class NotifySettingServiceImpl extends ServiceImpl<NotifySettingMapper, NotifySetting> implements NotifySettingService {
    private final NotifySettingConverter notifySettingConverter;

    @Override
    public void saveMailSetting(NotifySettingRequest request) {
        NotifyTypeEnum.validRequest(request);
        NotifySetting setting = notifySettingConverter.input2Entity(request);
        Integer id = request.getId();
        if (id != null) {
            Assert.isTrue(getById(id) != null, "不存在此数据！");
        }
        saveOrUpdate(setting);
    }

    @Override
    public NotifySettingVo getMailSetting() {
        Integer currentUserId = ContextCache.currentUserId();
        LambdaQueryWrapper<NotifySetting> queryWrapper = new LambdaQueryWrapper<NotifySetting>().eq(NotifySetting::getUserId, currentUserId);
        NotifySetting setting = getOne(queryWrapper);
        if (setting == null) {
            return null;
        }
        return notifySettingConverter.entity2Vo(setting);
    }
}
